//场景搭建
var stage = new createjs.Stage('playBoard');

//添加帧数
createjs.Ticker.setFPS(30);
createjs.Ticker.addEventListener('tick', stage);

//容器
var playBoard = new createjs.Container();
playBoard.x = 30;
playBoard.y = 30;
//容器添加到舞台中
stage.addChild(playBoard);

var arr = [
	[],
	[],
	[],
	[],
	[],
	[],
	[],
	[],
	[]
]; //二维数组表示地图坐标

var catPosition;
var mNone = -1;
var mLeft = 0;
var mUpLeft = 1;
var mUpRight = 2;
var mRight = 3;
var mDownRight = 4;
var mDownLeft = 5;


//绘制棋盘
function addCircles() {
	for (var Y = 0; Y < 9; Y++) {
		for (var X = 0; X < 9; X++) {
			var Yuan = new Circle();
			playBoard.addChild(Yuan);
			arr[X][Y] = Yuan;
			Yuan.X = X;
			Yuan.Y = Y;
			Yuan.x = Y % 2 ? X * 55 + 25 : X * 55;
			Yuan.y = Y * 55;

			//绘制猫
			if (X == 4 && Y == 4) {
				Yuan.setType(3);
				catPosition = Yuan;
			} else if (Math.random() < 0.1) {
				Yuan.setType(Circle.used);
			}
			Yuan.addEventListener('click', circleClick);
		}
	}
}

addCircles();

function circleClick(event) {
	if (event.target.getType() != Circle.cat) {
		event.target.setType(Circle.used);
	}

	if(catPosition.X == 0 || catPosition.Y==0 || catPosition.X==8 ||catPosition.Y == 8){
		alert('U lose');
		return;
	}

	var direction = getMoveDirection(catPosition);
	switch (direction) {
		case mLeft:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.X - 1][catPosition.Y];
			catPosition.setType(Circle.cat);
			break;
		case mUpLeft:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.Y % 2 ? catPosition.X : catPosition.X - 1][catPosition.Y - 1];
			catPosition.setType(Circle.cat);
			break;
		case mUpRight:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.Y % 2 ? catPosition.X : catPosition.X + 1][catPosition.Y - 1];
			catPosition.setType(Circle.cat);
			break;
		case mRight:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.X + 1][catPosition.Y];
			catPosition.setType(Circle.cat);
			break;
		case mDownRight:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.Y % 2 ? catPosition.X : catPosition.X + 1][catPosition.Y + 1];
			catPosition.setType(Circle.cat);
			break;
		case mDownLeft:
			catPosition.setType(Circle.unused);
			catPosition = arr[catPosition.Y % 2 ? catPosition.X : catPosition.X - 1][catPosition.Y + 1];
			catPosition.setType(Circle.cat);
			break;
		default:
			alert('you atr the winner');
	}
}

function getMoveDirection(cat) {
	var distanceMap = [];
	var able = true;
	for (var x = cat.X; x >= 0; x--) {
		if (arr[x][cat.Y].getType() == Circle.used) {
			able = false;
			distanceMap[mLeft] = cat.X - x;
			break;
		}
	}
	if (able) {
		return mLeft;
	}
	able = true;


	var x = cat.X,
		y = cat.Y;
	while (true) {
		if (arr[x][y].getType() == Circle.used) {
			able = false;
			distanceMap[mUpLeft] = cat.Y - y;
			break;
		}
		if (y % 2 == 0) {
			x--;
		}
		y--;
		if (y < 0 || x < 0) {
			break;
		}
	}
	if (able) {
		return mUpLeft;
	}
	able = true;


	x = cat.X, y = cat.Y;
	while (true) {
		if (arr[x][y].getType() == Circle.used) {
			able = false;
			distanceMap[mUpRight] = cat.Y - y;
			break;
		}
		if (y % 2 == 0) {
			x++;
		}
		y--;
		if (y < 0 || x > 8) {
			break;
		}
	}
	if (able) {
		return mUpRight;
	}
	able = true;

	for (var x = cat.X; x < 9; x++) {
		if (arr[x][cat.Y].getType() == Circle.used) {
			able = false;
			distanceMap[mRight] = x - cat.X;
			break;
		}
	}
	if (able) {
		return mRight;
	}
	able = true;

	x = cat.X, y = cat.Y;
	while (true) {
		if (arr[x][y].getType() == Circle.used) {
			able = false;
			distanceMap[mDownRight] = y - cat.Y;
			break;
		}
		if (y % 2 == 1) {
			x++;
		}
		y++;
		if (x > 8 || y > 8) {
			break;
		}
	}
	if (able) {
		return mDownRight;
	}
	able = true;

	x = cat.X, y = cat.Y;
	while (true) {
		if (arr[x][y].getType() == Circle.used) {
			able = false;
			distanceMap[mDownLeft] = y - cat.Y;
			break;
		}
		if (y % 2 == 0) {
			x--;
		}
		if (y > 8 || x < 0) {
			break;
		}
	}
	if (able) {
		return mDownLeft;
	}

	var maxDirection = -1;
	var maxVal = -1;
	for(var direction=0;direction<distanceMap.length;direction++){
		if(distanceMap[direction] > maxVal){
			maxVal = distanceMap[direction];
			maxDirection = direction;
		}
	}
	if(maxVal>1){
		return maxDirection;
	}else {
		return mNone;
	}
}